package com.example.opencv_demo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.opencv_demo.R;

import java.util.List;

public class TextAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private List<RunnableBean> mData;
    private Context context;

    public TextAdapter(List<RunnableBean> mData, Context context) {
        this.mData = mData;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TextViewHolder(LayoutInflater.from(context).inflate(R.layout.item_text, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TextViewHolder){
            ((TextViewHolder) holder).textView.setText(mData.get(position).content);
            final int p=position;
            ((TextViewHolder) holder).textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mData.get(p).runnable.run();
                    Toast.makeText(context,"完成",Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData==null?0:mData.size();
    }

    private static class TextViewHolder extends RecyclerView.ViewHolder{
        public TextView textView;
        public TextViewHolder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.tv_content);
        }
    }

    public static class RunnableBean{
        public String content;
        public Runnable runnable;

        public RunnableBean(String content, Runnable runnable) {
            this.content = content;
            this.runnable = runnable;
        }
    }
}
